package com.example.download;

import android.os.Environment;

import java.io.File;
import java.net.InetAddress;
import java.net.Socket;
import java.util.logging.Handler;

public class Main {
    public static void f() throws Exception {
        // 地址和端口号
        Socket s = new Socket(InetAddress.getByName(Constants.IP),
                Integer.parseInt(Constants.PORT));
        ClientActionController controller = new ClientActionController(s);

        boolean flag = true;
        boolean isEnd;
        boolean isfirstSendOk = false;
        // 头部信息
        while (true) {

            try {

                if (!isfirstSendOk) {

                    for (File f : new File(Constants.FILE_DIR).listFiles()) {
                        System.out.println(f.getName());
                    }
                    System.out.println(new File(Constants.FILE_DIR + "/123.pdf").exists());
                    controller.sendHeadInfo(Constants.FILE_DIR + "/123.pdf");
                    isfirstSendOk = true;
                }
                if (!flag) {
                    System.out.println("有错误发生");
                    isEnd = controller.receiveActionAfterError();
                    flag = true;
                } else {
                    isEnd = controller.receiveAction();
                }

            } catch (SocketException e) {
                System.out.println(e.log);
                flag = false;
                continue;
            }
            if (isEnd) {
                controller.close();
                s.close();
                System.out.println("结束 ");
                break;
            }
        }

    }
}
